package com.bookocean.domain.service.impl;

import com.bookocean.application.exception.BusinessException;
import com.bookocean.application.exception.WeChatErrorCodeEnum;
import com.bookocean.controller.dto.FavoriteDto;
import com.bookocean.controller.dto.CommentDto;
import com.bookocean.controller.dto.PageResultDto;
import com.bookocean.domain.repository.BookLikeRepository;
import com.bookocean.domain.repository.BookCommentRepository;
import com.bookocean.domain.repository.BookFavoriteRepository;
import com.bookocean.domain.service.InteractionDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 互动领域服务实现类
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InteractionDomainServiceImpl implements InteractionDomainService {

    private final BookLikeRepository bookLikeRepository;
    private final BookCommentRepository bookCommentRepository;
    private final BookFavoriteRepository bookFavoriteRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(Long targetId, Integer targetType, Long userId) {
        try {
            // 检查是否已经点赞
            if (bookLikeRepository.isLikedByUser(targetId, targetType, userId)) {
                throw new BusinessException(WeChatErrorCodeEnum.PARAM_ERROR, "您已经点过赞了");
            }
            
            bookLikeRepository.like(targetId, targetType, userId);
            log.info("用户点赞成功, targetId: {}, targetType: {}, userId: {}", targetId, targetType, userId);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("点赞失败, targetId: {}, targetType: {}, userId: {}, error: {}", 
                    targetId, targetType, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "点赞失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unlike(Long targetId, Integer targetType, Long userId) {
        try {
            // 检查是否已经点赞
            if (!bookLikeRepository.isLikedByUser(targetId, targetType, userId)) {
                throw new BusinessException(WeChatErrorCodeEnum.PARAM_ERROR, "您还未点赞");
            }
            
            bookLikeRepository.unlike(targetId, targetType, userId);
            log.info("用户取消点赞成功, targetId: {}, targetType: {}, userId: {}", targetId, targetType, userId);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("取消点赞失败, targetId: {}, targetType: {}, userId: {}, error: {}", 
                    targetId, targetType, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "取消点赞失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void favorite(Long targetId, Integer targetType, Long userId) {
        try {
            // 检查是否已经收藏
            if (bookFavoriteRepository.isFavoritedByUser(targetId, targetType, userId)) {
                throw new BusinessException(WeChatErrorCodeEnum.PARAM_ERROR, "您已经收藏过了");
            }
            
            bookFavoriteRepository.favorite(targetId, targetType, userId);
            log.info("用户收藏成功, targetId: {}, targetType: {}, userId: {}", targetId, targetType, userId);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("收藏失败, targetId: {}, targetType: {}, userId: {}, error: {}", 
                    targetId, targetType, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "收藏失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unfavorite(Long targetId, Integer targetType, Long userId) {
        try {
            // 检查是否已经收藏
            if (!bookFavoriteRepository.isFavoritedByUser(targetId, targetType, userId)) {
                throw new BusinessException(WeChatErrorCodeEnum.PARAM_ERROR, "您还未收藏");
            }
            
            bookFavoriteRepository.unfavorite(targetId, targetType, userId);
            log.info("用户取消收藏成功, targetId: {}, targetType: {}, userId: {}", targetId, targetType, userId);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("取消收藏失败, targetId: {}, targetType: {}, userId: {}, error: {}", 
                    targetId, targetType, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "取消收藏失败");
        }
    }

    @Override
    public PageResultDto<CommentDto> getComments(Long targetId, Integer targetType, Integer page, Integer pageSize) {
        try {
            return bookCommentRepository.getCommentsByTarget(targetId, targetType, page, pageSize);
        } catch (Exception e) {
            log.error("获取评论列表失败, targetId: {}, targetType: {}, error: {}", 
                    targetId, targetType, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "获取评论列表失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comment(Long targetId, Integer targetType, Long userId, String content, Long parentId) {
        try {
            bookCommentRepository.comment(targetId, targetType, userId, content, parentId);
            log.info("用户评论成功, targetId: {}, targetType: {}, userId: {}", targetId, targetType, userId);
        } catch (Exception e) {
            log.error("评论失败, targetId: {}, targetType: {}, userId: {}, error: {}", 
                    targetId, targetType, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "评论失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void replyComment(Long commentId, Long userId, String content) {
        try {
            bookCommentRepository.replyComment(commentId, userId, content);
            log.info("用户回复评论成功, commentId: {}, userId: {}", commentId, userId);
        } catch (Exception e) {
            log.error("回复评论失败, commentId: {}, userId: {}, error: {}", 
                    commentId, userId, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "回复评论失败");
        }
    }

    @Override
    public PageResultDto<FavoriteDto> getUserFavorites(Long userId, Integer targetType, Integer page, Integer pageSize) {
        try {
            return bookFavoriteRepository.getUserFavorites(userId, targetType, page, pageSize);
        } catch (Exception e) {
            log.error("获取用户收藏列表失败, userId: {}, targetType: {}, error: {}", 
                    userId, targetType, e.getMessage(), e);
            throw new BusinessException(WeChatErrorCodeEnum.SYSTEM_ERROR, "获取收藏列表失败");
        }
    }
}
